/*
 * MainWindow.java
 *
 * Created on July 23, 2008, 11:48 PM
 */

package org.imlinker.gui;

import java.awt.SystemTray;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import org.imlinker.core.Core;
import org.imlinker.list.CellRender;
import org.imlinker.list.ListManager;

public class MainWindow extends LocationSavedWindow {

	public MainWindow(Core core) {
		super();

		this.core = core;

		initComponents();

		setName("MainWindow");
		restoreLocation();

		new MainWindowListener(this);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(final WindowEvent e) {
				if (SystemTray.isSupported()) {

					setVisible(false);
				} else {
					System.exit(0);
				}
			}

		});

	}

	public void setListModel(ListManager listManager) {
		CellRender render = listManager.getCellRender();

		messageList.setModel(listManager
				.getListModelByName(ListManager.LISTNAME.MESSAGELIST));
		messageList.setCellRenderer(render);

		buddyList.setModel(listManager
				.getListModelByName(ListManager.LISTNAME.BUDDYLIST));
		buddyList.setCellRenderer(render);

		pluginList.setModel(listManager
				.getListModelByName(ListManager.LISTNAME.PLUGINLIST));
		pluginList.setCellRenderer(render);
	}

	public void setMessageNum(int number) {
		listTab.setTitleAt(0, "Message(" + number + ")");
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		listTab = new javax.swing.JTabbedPane();
		messageTab = new javax.swing.JScrollPane();
		messageList = new javax.swing.JList();
		buddyTab = new javax.swing.JScrollPane();
		buddyList = new javax.swing.JList();
		pluginTab = new javax.swing.JScrollPane();
		pluginList = new javax.swing.JList();
		javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
		statusLabel = new javax.swing.JLabel();
		iconLabel = new javax.swing.JLabel();
		javax.swing.JMenuBar jMenuBar1 = new javax.swing.JMenuBar();
		javax.swing.JMenu jMenu1 = new javax.swing.JMenu();
		javax.swing.JMenuItem preferenceItem = new javax.swing.JMenuItem();
		javax.swing.JMenuItem pluginItem = new javax.swing.JMenuItem();
		javax.swing.JMenuItem quitItem = new javax.swing.JMenuItem();
		javax.swing.JMenu jMenu2 = new javax.swing.JMenu();
		javax.swing.JCheckBoxMenuItem showOnlineItem = new javax.swing.JCheckBoxMenuItem();
		javax.swing.JMenu jMenu3 = new javax.swing.JMenu();
		javax.swing.JMenuItem aboutItem = new javax.swing.JMenuItem();

		java.util.ResourceBundle bundle = java.util.ResourceBundle
				.getBundle("org/imlinker/gui/Bundle"); // NOI18N
		setTitle(bundle.getString("MainWindow.title")); // NOI18N

		listTab.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);

		messageTab.setViewportView(messageList);

		listTab.addTab(bundle
				.getString("MainWindow.messageTab.TabConstraints.tabTitle"),
				messageTab); // NOI18N

		buddyTab.setViewportView(buddyList);

		listTab.addTab(bundle
				.getString("MainWindow.buddyTab.TabConstraints.tabTitle"),
				buddyTab); // NOI18N

		pluginTab.setViewportView(pluginList);

		listTab.addTab(bundle
				.getString("MainWindow.pluginTab.TabConstraints.tabTitle"),
				pluginTab); // NOI18N

		statusLabel.setText(bundle.getString("MainWindow.statusLabel.text")); // NOI18N

		iconLabel.setText(bundle.getString("MainWindow.iconLabel.text")); // NOI18N

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addComponent(iconLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												statusLabel,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												250, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(iconLabel)
														.addComponent(
																statusLabel))));

		jMenu1.setText(bundle.getString("MainWindow.jMenu1.text")); // NOI18N

		preferenceItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_E,
				java.awt.event.InputEvent.ALT_MASK));
		preferenceItem.setText(bundle
				.getString("MainWindow.preferenceItem.text")); // NOI18N
		jMenu1.add(preferenceItem);

		pluginItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_G,
				java.awt.event.InputEvent.ALT_MASK));
		pluginItem.setText(bundle.getString("MainWindow.pluginItem.text")); // NOI18N
		jMenu1.add(pluginItem);

		quitItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_T,
				java.awt.event.InputEvent.ALT_MASK));
		quitItem.setText(bundle.getString("MainWindow.quitItem.text")); // NOI18N
		jMenu1.add(quitItem);

		jMenuBar1.add(jMenu1);

		jMenu2.setText(bundle.getString("MainWindow.jMenu2.text")); // NOI18N

		showOnlineItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_S,
				java.awt.event.InputEvent.ALT_MASK));
		showOnlineItem.setSelected(true);
		showOnlineItem.setText(bundle
				.getString("MainWindow.showOnlineItem.text")); // NOI18N
		jMenu2.add(showOnlineItem);

		jMenuBar1.add(jMenu2);

		jMenu3.setText(bundle.getString("MainWindow.jMenu3.text")); // NOI18N

		aboutItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_A,
				java.awt.event.InputEvent.ALT_MASK));
		aboutItem.setText(bundle.getString("MainWindow.aboutItem.text")); // NOI18N
		aboutItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				java.awt.EventQueue.invokeLater(new Runnable() {
					public void run() { 
						new AboutWindow(MainWindow.this, false)
								.setVisible(true);
					}
				});
			}

		});
		jMenu3.add(aboutItem);

		jMenuBar1.add(jMenu3);

		setJMenuBar(jMenuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(listTab,
						javax.swing.GroupLayout.Alignment.TRAILING,
						javax.swing.GroupLayout.DEFAULT_SIZE, 301,
						Short.MAX_VALUE));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												jPanel1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												listTab,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												343, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	// Variables declaration - do not modify
	javax.swing.JList buddyList;

	javax.swing.JScrollPane buddyTab;

	javax.swing.JLabel iconLabel;

	javax.swing.JTabbedPane listTab;

	javax.swing.JList messageList;

	javax.swing.JScrollPane messageTab;

	javax.swing.JList pluginList;

	javax.swing.JScrollPane pluginTab;

	javax.swing.JLabel statusLabel;

	// End of variables declaration

	private Core core;

}
